package com.detroitchowfree;

import java.util.ArrayList;

import com.detroitchowfree.R;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



public class Main extends Activity 
implements OnTouchListener, OnClickListener, OnItemClickListener, IStoreServiceObserver{
    /** Called when the activity is first created. */
  
  ImageButton btnLocation;
  ImageButton btnRefresh;
  ImageButton btnSearch;
  
  
  public TextView tv1;
  LinearLayout layoutTags;
  private StoreService storeService=null;
  ArrayList<StoreVO> stores =null;
  private ListView storesListView=null;
  private ArrayAdapter<StoreVO> storesArrayAdapter=null;
  private ArrayList<String> tags=null;
  private final int buttonHeight=80;
  private final int buttonWidth=220;
  private Button _btnTag;
  private ProgressDialog pd;
  private Intent myIntent;

 
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnRefresh = (ImageButton)  findViewById(R.id.btnRefresh);
    btnRefresh.setOnClickListener(this);
    
    btnLocation = (ImageButton)  findViewById(R.id.btnLocation);
    btnLocation.setOnClickListener(this);
   
    btnSearch = (ImageButton)  findViewById(R.id.btnSearch);
    btnSearch.setOnClickListener(this);
    
    storesListView = (ListView) this.findViewById(R.id.lvStores);
    
    
    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    request.addTestDevice("014691441900D00F");
    AdView ad = (AdView) findViewById(R.id.ad);
    ad.loadAd(request);
    
    
    layoutTags = (LinearLayout) findViewById(R.id.layoutTags);
    storeService = ((StoreService)getApplicationContext());
    try{
    	stores = storeService.getStoresList();
    }catch(Exception e){
    	stores = null;
    }
    
    if(stores == null){
    	storeService.registerUpdateStoresObserver(this);
    	callProgressBar();
    	storeService.initStores();
    }else{
    	onStoreManagerUpdate();
    }
    	
    
  }

  public void onClick(View v) {
    // TODO Auto-generated method stub
    myIntent=null;
    if(v.equals(btnRefresh)) {
      LogUtil.log("onClick", "Click event is btnRefresh");
      storeService.registerUpdateStoresObserver(this);
      layoutTags.removeAllViewsInLayout();
      storesListView.removeAllViewsInLayout();
      callProgressBar();
  		storeService.refreshStores();
    }else if(v.getTag() != null) {
      if(v.getTag().toString().toUpperCase().equals(StoreService.ALL_TAG))
      	stores = storeService.getStoresList();
       else
      	 stores = storeService.getStoresList((String)v.getTag());
    	refreshDirectory();
    }if(v.equals(btnLocation)){
    	myIntent = new Intent(this, MapScreen.class);
    	myIntent.putExtra(MapScreen.INTENT_KEY, AppIntent.LOCATION);
    	startActivityForResult(myIntent, 0);
    }if(v.equals(btnSearch)){
    	myIntent = new Intent(this, SearchActivity.class);
    	startActivityForResult(myIntent, 0);
    }else{
      LogUtil.log("Main", "Click event is NOT Dir");
    }
  }
  
  

	public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
	  // TODO Auto-generated method stub
		int selectedStoreId =  stores.get(index).mapGroupId ;
		int realStoreId =  stores.get(index).storeId ;
		// get the intent
    myIntent = new Intent(this, MapScreen.class);
    myIntent.putExtra(MapScreen.INTENT_KEY, AppIntent.SELECT_STORE);
    myIntent.putExtra(StoreService.SELECTED_STORE_KEY, selectedStoreId);
    myIntent.putExtra(StoreService.SELECTED_STORE_OVERRIDE, realStoreId);
    startActivityForResult(myIntent, 0);
	}

	public void onStoreManagerUpdate() {
	  // TODO Auto-generated method stub
		LogUtil.log("Main",  "onStoreManagerUpdate");
		storeService.unRegisterUpdateStoresObserver(this);
		tags = storeService.getTags();
    if(tags != null){
    	for(String tag: tags){
    		_btnTag = (Button)getLayoutInflater().inflate(R.layout.linkitem, null);
    		_btnTag.setText(tag);
    		_btnTag.setTag(tag);
    		_btnTag.setOnClickListener(this);
    		_btnTag.setOnTouchListener(this);
    		ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(tag.length() * 25, 75);
    		_btnTag.setLayoutParams(layoutParams);
    		layoutTags.addView(_btnTag);
    	}
    }
    if(pd != null)
      pd.dismiss();
    stores = storeService.getStoresList();
    if(stores == null)
    	LogUtil.log("Main",  "onStoreManagerUpdate stores is null");
    else
    	LogUtil.log("Main",  "onStoreManagerUpdate stores is not null: " + stores.size());
    refreshDirectory();
  }
  
  public void refreshDirectory() {
  	LogUtil.log("Main refreshDirectory", "Stores Size: " + stores.size());
    storesArrayAdapter = new StoresArrayAdapter(this, R.layout.directoryitem, R.id.txtstoretitle, stores);
    if(storesArrayAdapter != null){
    	storesListView.setAdapter(storesArrayAdapter);
      storesListView.setTextFilterEnabled(true);
      storesListView.setOnItemClickListener(this);
    }
    
   
    
  }

	public void onArticleUpdate(ArrayList<Article> result) {
	  // TODO Auto-generated method stub
	  
  }

	public boolean onTouch(View v, MotionEvent event) {
	  if (v.getTag() != null){
			if(v.getTag().toString().toUpperCase().equals(StoreService.ALL_TAG))
      	stores = storeService.getStoresList();
       else
      	 stores = storeService.getStoresList((String)v.getTag());
			refreshDirectory();
		}
	  return false;
  }
	
	private void callProgressBar(){
		pd = ProgressDialog.show(this, "Working..","Refreshing the Detroit Chow list", true,
        false);
	}
  
}